<!--
  智慧社区管理系统 - 维修工单完成弹窗
  @author Wu.Liang
  @date 2025-01-30
-->
<template>
  <el-dialog
    v-model="dialogVisible"
    title="完成维修工单"
    width="500px"
    :close-on-click-modal="false"
    @close="handleClose"
  >
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
    >
      <el-form-item label="工单信息">
        <div class="order-info">
          <p><strong>工单号：</strong>{{ repairOrder?.orderNo }}</p>
          <p><strong>维修标题：</strong>{{ repairOrder?.title }}</p>
          <p><strong>维修地址：</strong>{{ repairOrder?.repairAddress }}</p>
          <p><strong>维修人员：</strong>{{ repairOrder?.repairmanName }}</p>
        </div>
      </el-form-item>
      
      <el-form-item label="维修结果" prop="result">
        <el-input
          v-model="formData.result"
          type="textarea"
          :rows="4"
          placeholder="请详细描述维修结果，包括维修内容、更换的配件、测试情况等"
          maxlength="500"
          show-word-limit
        />
      </el-form-item>
      
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="formData.remark"
          type="textarea"
          :rows="3"
          placeholder="请输入完成备注（可选）"
          maxlength="200"
          show-word-limit
        />
      </el-form-item>
    </el-form>
    
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleSubmit" :loading="loading">
          确认完成
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, watch, computed } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import type { RepairOrder } from '../types/repair'

// 作者: Wu.Liang

// Props
interface Props {
  modelValue: boolean
  repairOrder: RepairOrder | null
  loading?: boolean
}

const props = withDefaults(defineProps<Props>(), {
  loading: false
})

// Emits
const emit = defineEmits<{
  'update:modelValue': [value: boolean]
  'confirm': [data: any]
}>()

// 响应式数据
const formRef = ref<FormInstance>()

// 表单数据
const formData = reactive({
  result: '',
  remark: ''
})

// 表单验证规则
const formRules: FormRules = {
  result: [
    { required: true, message: '请填写维修结果', trigger: 'blur' },
    { min: 10, message: '维修结果至少10个字符', trigger: 'blur' }
  ]
}

// 计算属性
const dialogVisible = computed({
  get: () => props.modelValue,
  set: (value) => emit('update:modelValue', value)
})

// 监听器
watch(() => props.modelValue, (newVal) => {
  if (newVal) {
    resetForm()
  }
})

// 重置表单
const resetForm = () => {
  Object.assign(formData, {
    result: '',
    remark: ''
  })
  formRef.value?.clearValidate()
}

// 提交表单
const handleSubmit = async () => {
  if (!formRef.value) return
  
  try {
    await formRef.value.validate()
    
    const submitData = {
      result: formData.result,
      remark: formData.remark || undefined
    }
    
    emit('confirm', submitData)
  } catch (error) {
    console.error('表单验证失败:', error)
  }
}

// 关闭弹窗
const handleClose = () => {
  dialogVisible.value = false
}
</script>

<style lang="scss" scoped>
.order-info {
  background-color: #f5f7fa;
  padding: 12px;
  border-radius: 4px;
  border-left: 4px solid #67c23a;
  
  p {
    margin: 0 0 8px 0;
    line-height: 1.5;
    
    &:last-child {
      margin-bottom: 0;
    }
    
    strong {
      color: #303133;
    }
  }
}

.dialog-footer {
  text-align: right;
}

:deep(.el-rate) {
  .el-rate__text {
    margin-left: 10px;
    font-size: 14px;
    color: #606266;
  }
}
</style> 